package com.blueocean_health.care.management.interceptor;

import java.io.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

/**
 * @version 1.0
 * @author: zwg.BlueOcean
 * @date 2018/7/3 19:54
 * @description
 */
public class CustomHttpServletResponseWrapper extends HttpServletResponseWrapper {

    private PrintWriter cachedWriter;
    private CharArrayWriter bufferedWriter;

    public CustomHttpServletResponseWrapper(HttpServletResponse response) throws UnsupportedEncodingException {
        super(response);
        bufferedWriter = new CharArrayWriter();
        cachedWriter = new PrintWriter(bufferedWriter);

    }

    @Override
    public PrintWriter getWriter() {
        return cachedWriter;
    }


    public String getResult() {
        return bufferedWriter.toString();
    }

    public String getContent() throws IOException {
        String result = getResult();
        PrintWriter out = getWriter();
        out.write(result);
        out.flush();
        out.close();
        return result;
    }

}
